import api from './api';

export const weatherService = {
  // 获取城市天气
  async getCityWeather(cityId) {
    try {
      const response = await api.get(`/weather/${cityId}`);
      console.log('weatherService.getCityWeather: Full response object after api.get():', response);
      console.log('weatherService.getCityWeather: Data being returned from service:', response);
      return response;
    } catch (error) {
      console.error('获取城市天气失败:', error);
      throw error;
    }
  },

  // 获取用户默认城市的天气
  getDefaultCityWeather() {
    return api.get('/weather/default');
  },

  // 获取所有城市列表
  getCities() {
    return api.get('/cities');
  },

  // 设置默认城市
  setDefaultCity(cityId) {
    return api.post('/users/default-city', { cityId });
  },

  // 根据经纬度获取城市信息
  getCityByCoordinates(latitude, longitude) {
    return api.get(`/cities/by-coordinates?latitude=${latitude}&longitude=${longitude}`);
  },

  // 根据城市名称获取城市信息
  searchCityByName(cityName) {
    return api.get(`/cities/by-name?cityName=${cityName}`);
  },

  async getCityForecast(cityId) {
    try {
      const response = await api.get(`/weather/${cityId}/forecast`);
      console.log('weatherService.getCityForecast: Full response object after api.get():', response);
      console.log('weatherService.getCityForecast: Data being returned from service:', response);
      return response;
    } catch (error) {
      console.error('获取天气预报失败:', error);
      throw error;
    }
  },

  // 获取历史天气
  async getHistoricalWeather(location, date) {
    try {
      const response = await api.get(`/weather/history/${location}?date=${date}`);
      console.log('weatherService.getHistoricalWeather: Full response object after api.get():', response);
      console.log('weatherService.getHistoricalWeather: Data being returned from service:', response);
      return response;
    } catch (error) {
      console.error('获取历史天气失败:', error);
      throw error;
    }
  },
}; 